/**
 * //  旋转图片 获得四个顶点坐标
 * @param {*} top 
 * @param {*} left 
 * @param {*} width 
 * @param {*} height 
 * @param {*} rotationAngle 
 * @returns 
 */
export const rotateRectanglePoints =(top, left, width, height, rotationAngle =0) =>{
    // 计算长方形的中心点坐标
    const centerX = left + width / 2;
    const centerY = top + height / 2;
    // 将角度转换为弧度
    const angleInRadians = (rotationAngle * Math.PI) / 180;
    // 计算旋转矩阵
    const cosTheta = Math.cos(angleInRadians);
    const sinTheta = Math.sin(angleInRadians);
    // 计算每个顶点相对于中心点的坐标
    const topLeftX = -width / 2;
    const topLeftY = -height / 2;
    const topRightX = width / 2;
    const topRightY = -height / 2;
    const bottomLeftX = -width / 2;
    const bottomLeftY = height / 2;
    const bottomRightX = width / 2;
    const bottomRightY = height / 2;
    // 应用旋转矩阵到每个顶点
    const rotatedTopLeftX =
      centerX + (topLeftX * cosTheta - topLeftY * sinTheta);
    const rotatedTopLeftY =
      centerY + (topLeftX * sinTheta + topLeftY * cosTheta);
    const rotatedTopRightX =
      centerX + (topRightX * cosTheta - topRightY * sinTheta);
    const rotatedTopRightY =
      centerY + (topRightX * sinTheta + topRightY * cosTheta);
    const rotatedBottomLeftX =
      centerX + (bottomLeftX * cosTheta - bottomLeftY * sinTheta);
    const rotatedBottomLeftY =
      centerY + (bottomLeftX * sinTheta + bottomLeftY * cosTheta);
    const rotatedBottomRightX =
      centerX + (bottomRightX * cosTheta - bottomRightY * sinTheta);
    const rotatedBottomRightY =
      centerY + (bottomRightX * sinTheta + bottomRightY * cosTheta);
    // 返回旋转后的四个顶点坐标
    return {
        A: [parseFloat(rotatedTopRightX.toFixed(0)), parseFloat(rotatedTopRightY.toFixed(0))],
        B: [parseFloat(rotatedBottomRightX.toFixed(0)), parseFloat(rotatedBottomRightY.toFixed(0))],
        C: [parseFloat(rotatedBottomLeftX.toFixed(0)), parseFloat(rotatedBottomLeftY.toFixed(0))],
      }

  }
  //  计算斜率
  export const calculateSlope =(x1, y1, x2, y2)=> {
    if (x2 - x1 === 0) {
      return (y2 > y1) ? Infinity : -Infinity;
  } else {
      return (y2 - y1) / (x2 - x1);
  }
  }
// export function findDuplicatePointsKey(targetPoint) {
//   const pointsMap = {};
//   // 遍历每个对象中的顶点坐标
//   for (const obj of targetPoint) {
//       for (const pointKey in obj) {
//           const point = obj[pointKey];
//           const pointString = JSON.stringify(point);

//           // 如果该顶点坐标已经存在于pointsMap中，则将其添加到结果数组中
//           if (pointsMap[pointString]) {
//               pointsMap[pointString].push(pointKey);
//           } else {
//               pointsMap[pointString] = [pointKey];
//           }
//       }
//   }

//   // 从pointsMap中提取出具有多个出现次数的顶点坐标及对应的A、B、C点
//   const duplicates = {};
//   for (const [pointString, keys] of Object.entries(pointsMap)) {
//       if (keys.length > 1) {
        
//           duplicates['duplicatePoint'] = JSON.parse(pointString);
//           duplicates['rectIndex'] = keys;
//       }
//   }

//   return duplicates;
// }
export function findDuplicatePointsValue(targetPoint) {
  const pointsMap = {};

  // 遍历每个对象中的顶点坐标
  for (const obj of targetPoint) {
      for (const pointKey in obj) {
          const point = obj[pointKey];

          // 将顶点坐标转换为字符串形式，以便进行比较
          const pointString = JSON.stringify(point);

          // 如果该顶点坐标已经存在于pointsMap中，则将其添加到结果数组中
          if (pointsMap[pointString]) {
              pointsMap[pointString].push(point);
          } else {
              pointsMap[pointString] = [point];
          }
      }
  }

  // 从pointsMap中提取出具有多个出现次数的顶点坐标
  const duplicates = Object.values(pointsMap).filter(points => points.length > 1);

  return duplicates;
}
export function findMaxYPriorityPoint(targetPoint) {
  let maxY = -Infinity;
  let minX = Infinity;
  let maxYPriorityPoint = null;
  for (const obj of targetPoint) {
      for (const pointKey in obj) {
          const [x, y] = obj[pointKey];
          if (y > maxY || (y === maxY && x < minX)) {
              maxY = y;
              minX = x;
              maxYPriorityPoint = obj[pointKey];
          }
      }
  }

  return maxYPriorityPoint;
}

/**
 * 通过两个三角形相交线部分的面积来判断，学生是否在叠角
 * @param {*} triangles 
 * @returns 
 */
export function trianglesIntersect(triangles) {
   // 计算两条直线的交点
function lineIntersection(p1, q1, p2, q2) {
  var x1 = p1[0], y1 = p1[1];
  var x2 = q1[0], y2 = q1[1];
  var x3 = p2[0], y3 = p2[1];
  var x4 = q2[0], y4 = q2[1];
  
  var det = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4);
  if (det == 0) return null;  // 平行线或共线

  var t = ((x1-x3)*(y3-y4) - (y1-y3)*(x3-x4)) / det;
  var u = -((x1-x2)*(y1-y3) - (y1-y2)*(x1-x3)) / det;
  
  if (t >= 0 && t <= 1 && u >= 0 && u <= 1) {
      var x = x1 + t * (x2 - x1);
      var y = y1 + t * (y2 - y1);
      return [x, y];
  }
  return null;  // 不相交
}

// 检查两个三角形是否相交，并返回相交区域的顶点
function getIntersectionPoints(triangle1, triangle2) {
  var intersectionPoints = [];
  for (var i = 0; i < 3; i++) {
      for (var j = 0; j < 3; j++) {
          var intersection = lineIntersection(
              triangle1[i], triangle1[(i + 1) % 3],
              triangle2[j], triangle2[(j + 1) % 3]
          );
          if (intersection) {
              intersectionPoints.push(intersection);
          }
      }
  }
  return intersectionPoints;
}

// 计算多边形的面积
function polygonArea(points) {
  var area = 0;
  var j = points.length - 1;
  for (var i = 0; i < points.length; i++) {
      area += (points[j][0] + points[i][0]) * (points[j][1] - points[i][1]);
      j = i;
  }
  return Math.abs(area / 2);
}

// 获取交点
var intersectionPoints = getIntersectionPoints(
  [triangles[0]['A'], triangles[0]['B'], triangles[0]['C']],
  [triangles[1]['A'], triangles[1]['B'], triangles[1]['C']]
);

// 计算相交部分面积
var intersectionArea = polygonArea(intersectionPoints);
let flag = false;
if(intersectionArea > 100) {
  flag = true;
}else if(intersectionArea < 100) {
  flag = false;
}
return flag;
}



export function getRandomColor(num) {
  // 生成随机的RGB值
  const data = ['#223344', '#0330fc', '#445588', '#fc03f4', '#fcd303', '#03fcfc', '#8899cc', '#99aadd', '#aabbee', '#bbccff'];
  // 拼接成颜色值并返回
  return data[num];
}
/**
 * 
 * @param {*} arr1 角度
 * @param {*} arr2 索引
 */
export function sortAngleHandler(array1, array2) {
  // 检查输入是否为数组以及数组是否为空
  if (!Array.isArray(array1) || !Array.isArray(array2) || array1.length === 0 || array2.length === 0) {
    console.error('Invalid input provided.');
    return [];
  }
  let sortedArray1 = []
    sortedArray1[minValue] = array1[minIndex];
    array1=array1.filter((item,index)=>index !==minIndex);
    array2 .filter((item,index)=>index !==minIndex).forEach((item,index)=>{
     sortedArray1[item] = array1[index];
   })
  return sortedArray1;
}








